package com.clkj.zhonghui.controller.order;

import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.pojo.order.MainOrder;
import com.clkj.zhonghui.pojo.order.Order;
import com.clkj.zhonghui.service.order.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/07/11 13:48:47
 **/
@RestController
@RequestMapping(value = "/order")
@Api(value = "/order", tags = {"订单管理"})
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;
    @PostMapping("/addOrder")
    @ApiOperation(value = "app--添加订单")
    public Result addOrder(@RequestBody MainOrder mainOrder){
        try {
            Map map =  orderService.addOrder(mainOrder);
            return new Result(true, StatusCode.OK,"添加成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/addMiniOrder")
    @ApiOperation(value = "小程序--添加订单(中汇胎美购买套餐)")
    public Result addMiniOrder(@RequestBody MainOrder mainOrder){
        try {
            Map map =  orderService.addMiniOrder(mainOrder);
            return new Result(true, StatusCode.OK,"添加成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/addMiniProductOrder")
    @ApiOperation(value = "小程序--添加订单(中汇胎美普通商品)")
    public Result addMiniProductOrder(@RequestBody MainOrder mainOrder){
        try {
            Map map =  orderService.addMiniProductOrder(mainOrder);
            return new Result(true, StatusCode.OK,"添加成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/listOrder")
    @ApiOperation(value = "app--获取订单列表")
    public Result listOrder(@ApiParam(name="userId",value = "用户ID")@RequestParam Integer userId,
                            @ApiParam(name="shopId",value = "门店ID")@RequestParam Integer shopId,
                            @ApiParam(name="page",value = "页码")@RequestParam Integer page,
                            @ApiParam(name="size",value = "每页条数")@RequestParam Integer size,
                            @ApiParam(name="flag",value = "订单状态，全部为-1")@RequestParam Integer flag){
        try {
            PageInfo map =  orderService.listOrder(userId,shopId,page,size,flag);
            return new Result(true, StatusCode.OK,"添加成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/listAllOrder")
    @ApiOperation(value = "后台--获取订单列表")
    public Result listAllOrder(@ApiParam(name="shopId",value = "门店ID")@RequestParam Integer shopId,
                            @ApiParam(name="page",value = "页码")@RequestParam Integer page,
                            @ApiParam(name="size",value = "每页条数")@RequestParam Integer size,
                            @ApiParam(name="flag",value = "订单状态，全部为-1")@RequestParam Integer flag,
                               @ApiParam(name="type",value = "查询的内容")@RequestParam String type,
                               @ApiParam(name="value",value = "查询的值")@RequestParam String value  ){
        try {
            PageInfo map =  orderService.listAllOrder(shopId,page,size,flag,type,value);
            return new Result(true, StatusCode.OK,"添加成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/sendOrder")
    @ApiOperation(value = "后台--订单发货")
    public Result sendOrder(@ApiParam(name="orderNumber",value = "订单号")@RequestParam String orderNumber,
                               @ApiParam(name="wuCompany",value = "物流公司")@RequestParam String wuCompany,
                               @ApiParam(name="wuOrder",value = "物流单号")@RequestParam String wuOrder,
                               @ApiParam(name="wuCode",value = "物流编码")@RequestParam String wuCode  ){
        try {
            orderService.sendOrder(orderNumber,wuCompany,wuOrder,wuCode);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/orderDetail")
    @ApiOperation(value = "后台--订单详情")
    public Result orderDetail(@ApiParam(name="orderNumber",value = "订单号")@RequestParam String orderNumber  ){
        try {
            List<Order> orderList = orderService.orderDetail(orderNumber);
            return new Result(true, StatusCode.OK,"添加成功",orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("/editOrderStatus")
    @ApiOperation(value = "修改订单状态")
    public Result editOrderStatus(Integer mainId,Integer orderStatus){
        try {
            orderService.editOrderStatus(mainId,orderStatus);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
}
